//
//  CYShopCartTopView.m
//  CaiYun
//
//  Created by 今日信息科技 on 2017/7/31.
//  Copyright © 2017年 今日信息科技. All rights reserved.
//

#import "CYShopCartTopView.h"
@interface CYShopCartTopView(){
    ShopCartTopViewBlock _myblock;
}
@property (nonatomic, strong) CYLabel *dateLBL;//距离开奖截止时间
@end
@implementation CYShopCartTopView
- (instancetype)initWithColor:(UIColor *)color block:(ShopCartTopViewBlock)block{
    self = [super initWithColor:color];
    if (self) {
        _myblock = block;
        CGFloat height = ceil([@"截止期" sizeWithAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:AL(PT(28))]}].height);
        self.bounds = CGRectMake(0.0, 0.0, SCREEN_WIDTH, height + AL(PT(30)) + AL(PT(26)) + AL(PT(24)) + AL(PT(66)));
        [self addSubButton];
    }
    return self;
}

- (CYLabel *)dateLBL{
    if (!_dateLBL) {
        _dateLBL = [[CYLabel alloc]initWithText:_date size:AL(PT(28)) textcolor:ColorHex(0xff5b05)];
        [self addSubview:_dateLBL];
        
        [_dateLBL mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self).with.offset(AL(PT(30)));
            make.centerX.equalTo(self);
            make.size.mas_equalTo(_dateLBL.bounds.size);
        }];
    }
    return _dateLBL;
}
//    self.topView.date =@"距45期截止:06:04";
- (void)setDate:(NSString *)date{
    if (_date != date)
        _date = date;
    self.dateLBL.text = _date;
    [self updateDateLBLBounds];
}

- (void)setTime:(NSString *)time{
    if (_time != time)
        _time = time;
    self.dateLBL.text = [NSString stringWithFormat:@"%@%@",_date,_time];
    [self updateDateLBLBounds];
}

- (void)addSubButton{
    
    CGFloat width = AL(PT(185));
    CGFloat height = AL(PT(66));
    CGFloat offsetX = (SCREEN_WIDTH - 3 * width) / 4.0;
    NSArray *array = @[@"随机1注",@"随机5注",@"自选号码"];
    for (int i = 0 ; i < array.count; i ++) {
        CYButton *button =[[CYButton alloc]initWithTitle:array[i] titleColor:THEME_COLOR backgroundColor:[UIColor clearColor] cornerRadius:AL(5.0)];
        [button addTarget:self action:@selector(actionForButton:) forControlEvents:UIControlEventTouchUpInside];
        [button setTitleColor:ColorHex(0xe0e0e0) forState:UIControlStateHighlighted];
        button.titleLabel.font = [UIFont systemFontOfSize:AL(PT(30))];
        button.layer.borderColor = ColorHex(0xe0e0e0).CGColor;
        button.layer.borderWidth = 0.5;
        button.tag = 200 + i;
        [self addSubview:button];
        
        WS(self)
        [button mas_makeConstraints:^(MASConstraintMaker *make) {
            make.bottom.equalTo(weakSelf.mas_bottom).with.offset(-AL(PT(24)));
            make.left.equalTo(weakSelf).with.offset(offsetX + (width + offsetX) * i);
            make.size.mas_equalTo(CGSizeMake(width, height));
        }];
    }
}

- (void)updateDateLBLBounds{

[_dateLBL mas_updateConstraints:^(MASConstraintMaker *make) {
    make.size.mas_equalTo(_dateLBL.bounds.size);
}];

}
- (void)actionForButton:(CYButton *)sender{
    
    if (sender.tag == 200)
        _myblock(SuiJiYiZhu);
    else if (sender.tag == 201)
        _myblock(SuiJiWuZhu);
    else if (sender.tag == 202)
        _myblock(ZiXuanHaoMa);
    
}

/*
 // Only override drawRect: if you perform custom drawing.
 // An empty implementation adversely affects performance during animation.
 - (void)drawRect:(CGRect)rect {
 // Drawing code
 }
 */

@end
